// 商品路由表

// 加载模块
const express = require('express');
const goodsModel = require('../db/models/goodsModel.js');

// 创建路由表
let router = express.Router();

// 向路由表添加内容：

router.get('/add',(req,res)=>{// f5
    // goodsModel.insertMany() // 增加数据
    res.send('add is ok');
})
router.get('/del',(req,res)=>{// f6
    res.send('del is ok');
})
router.get('/udate',(req,res)=>{// f7
    res.send('udate is ok');
})
router.get('/search',(req,res)=>{// f8
    res.send('search is ok');
})


router.get('/test1',(req,res)=>{// f8
    res.send({"name":"老王"});
})
router.get('/test2',(req,res)=>{// f8
    let {a,b} = req.query;
    res.send({"name":"老王","arr":[a,b]});
})
router.post('/test3',(req,res)=>{// f8
    res.send({"name":"老李"});
})
router.post('/test4',(req,res)=>{// f8
    let {a,b} = req.body;
    res.send({"name":"老李","arr":[a,b]});
})

router.get('/getmoney',(req,res)=>{
    let {msg,money} = req.query;

    if (money > 5000) {
        res.send({"code":0,"msg":"不同意，预算超标！","money":0});
    } else {
        res.send({"code":1,"msg":"同意，部门活动经费！","money":money});
    }
})

router.get('/cookie',(req,res)=>{
    let {a,b} = req.query;

    // 获取到请求头中的cookie数据
    console.log( req.cookies );// { user1: 'xiaocuo', user2: 'xiaohua' }

    // res.cookie(key,val,{配置}) 方法会在响应头中设置cookie
    res.cookie('user3','xiaoqiang',{
        maxAge: 1000*60*60,// 毫秒
        domain: 'localhost',
        path: '/'
    });

    res.send([a,b]);
})


router.get('/session',(req,res)=>{
    let {a,b} = req.query;

    // 根据 session id 匹配到的 session data
    // console.log( req.session );
    // req.session = {
    //     cookie: {
    //       path: '/',
    //       _expires: 2023-09-12T08:46:35.741Z,
    //       originalMaxAge: 1800000,
    //       httpOnly: true,
    //       domain: 'localhost'
    //     }
    // }

    if (a === 'xiaocuo' && b === '123123') {
        // 登录成功，向session data中存储信息
        req.session.login = 'is login';
        req.session.user = a;
    }
    console.log( req.session );
    // req.session = {
    //     cookie: {
    //       path: '/',
    //       _expires: 2023-09-12T08:54:52.790Z,
    //       originalMaxAge: 1800000,
    //       httpOnly: true,
    //       domain: 'localhost'
    //     },
    //     login: 'is login',
    //     user: 'xiaocuo'
    // }
      
    res.send([a,b]);
})

// 对外暴露接口
module.exports = router;
